<?php


include 'db-login.php';

//Performing SQL query
//Select all of the prescription medications from the pharmacy
//database that are still being taken and were picked up in the last 6
//months
//Assume you cannot write to pharmacy

$query = 'SELECT pharmacy.MedicationType, pharmacy.MedicationSubType,
pharmacy.MedicationIdentity, pharmacy.GenericIngredients,
pharmacy.UnitStrength, pharmacy.DosageForm, pharmacy.Route,
pharmacy.DispensedQuantity, pharmacy.RefillsAuthorized,
pharmacy.DosingFrequency, pharmacy.DosingQuantity,
pharmacy.DosingInstructions, pharmacy.FullTextDescription,
pharmacy.OutOfPocketCostUS, pharmacy.AnnotationReferences,
pharmacy.DosingStatus, pharmacy.EffectiveStartDate,
pharmacy.EffectiveEndDate, pharmacy.Provenance,
medications.inList FROM pharmacy LEFT JOIN medications ON
pharmacy.MedicationType = medications.MedicationType AND
pharmacy.MedicationSubType = medications.MedicationSubType AND
pharmacy.MedicationIdentity = medications.MedicationIdentity AND
pharmacy.GenericIngredients = medications.GenericIngredients AND
pharmacy.UnitStrength = medications.UnitStrength AND
pharmacy.DosageForm = medications.DosageForm AND
pharmacy.Route = medications.Route AND
pharmacy.DispensedQuantity = medications.DispensedQuantity AND
pharmacy.RefillsAuthorized = medications.RefillsAuthorized AND
pharmacy.DosingFrequency = medications.DosingFrequency AND
pharmacy.DosingQuantity = medications.DosingQuantity AND
pharmacy.DosingInstructions = medications.DosingInstructions AND
pharmacy.FullTextDescription = medications.FullTextDescription AND
pharmacy.OutOfPocketCostUS = medications.OutOfPocketCostUS AND
pharmacy.AnnotationReferences = medications.AnnotationReferences AND
pharmacy.DosingStatus = medications.DosingStatus AND
pharmacy.EffectiveStartDate = medications.EffectiveStartDate AND
pharmacy.EffectiveEndDate = medications.EffectiveEndDate AND
pharmacy.Provenance = medications.Provenance WHERE pharmacy.MedicationType = \'prescription\' AND pharmacy.EffectiveEndDate is NULL AND DATE_ADD(CURRENT_DATE, INTERVAL -6 MONTH) < pharmacy.EffectiveStartDate';

//echo $query."<BR>";

$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing Results in Medication Page for prescriptions-select.php -
// part of wizard
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
  $value = $row['MedicationType'].".".$row['MedicationSubType'].".".$row['MedicationIdentity'].".".$row['GenericIngredients'].".".$row['UnitStrength'].".".$row['DosageForm'].".".$row['Route'].".".$row['DispensedQuantity'].".".$row['RefillsAuthorized'].".".$row['DosingFrequency'].".".$row['DosingQuantity'].".".$row['DosingInstructions'].".".$row['FullTextDescription'].".".$row['OutOfPocketCostUS'].".".$row['AnnotationReferences'].".".$row['DosingStatus'].".".$row['EffectiveStartDate'].".".$row['EffectiveEndDate'].".".$row['Provenance'].".".$row['inList'];
  echo "<div id=\"one-med\">";

  echo "MedIdentity = ".$row['MedicationIdentity'];
    echo "inList= ".$row['inList']."<BR>";
  if ($row['inList'] == 1){
  echo "<input class=\"floatleft\" type=\"checkbox\" name=\"RxMeds[]\" value=\"".$value."\" checked>";
} else {

  echo "<input class=\"floatleft\" type=\"checkbox\" name=\"RxMeds[]\" value=\"".$value."\">";
}
  // Assuming file names for medications are named with the MedID & jpg form
  $filename = "./pics/".$row['MedicationIdentity'].".jpg";

  if (file_exists($filename)){
    echo "<img class=\"floatleft\" src=\"".$filename."\" alt=\"".$row['MedicationIdentity']."\" height=\"100\">";
  } else {
    echo "<div class=\"NABox\">N/A</div>";
  }
  echo "<p class=\"floatleft\">".$row['MedicationIdentity']."<BR>".$row['GenericIngredients']."<BR>".$row['UnitStrength']."</p></div>\n\n";
}
include './includes/db-close.php';

?>